package week_2.practice;

import week_1.practice.EmptyCollectionException;
import week_1.practice.LinearNode;

public class LinkedQueue<T> implements  QueueADT<T>
{
    private int count;
    private LinearNode<T> head,tail;

    /*Creates an empty queue.*/
    public LinkedQueue()
    {
        count=0;
        head = tail =null;
    }
    /*Adds the specified element  to the tail of this queue.
    * @param element the  element to be added to the tail of this queue*/
    public void enqueue(T element)
    {
        LinearNode<T> node = new LinearNode<T>(element);

        if(isEmpty())
            head = node;
        else
            tail.setNext(node);
        tail=node;
        count++;
    }
    /*Removes the element at the front of this queue and returns a
    * reference to it .Throws an EmptyCollectionException if the
    * queue id empty.
    * */
    public T dequeue() throws EmptyCollectionException
    {
        if(isEmpty())
            throw new EmptyCollectionException("queue");

        T result = head.getElement();
        head = head.getNext();
        count--;

        if(isEmpty())
            tail = null;

        return result;
    }

    @Override
    public T first() {
        return head.getElement();
    }

    @Override
    public boolean isEmpty() {
        if(count!=0)
            return false;
        else
            return true;
    }

    @Override
    public int size() {
        return count;
    }

    public String toString()
    {
        String result = "";
        for(int a = 0;a<count;a++)
        {
            result += head.getElement();
            head = head.getNext();
        }
        return result;
    }
}
